import requests
from lxml import etree
from time import sleep
import string

if __name__ == '__main__':
    url = 'https://bj.58.com/chuzu/'
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
               '(KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31'
               }

    page_text = requests.get(url=url, headers = headers).text
    tree = etree.HTML(page_text)

    title_name = tree.xpath('/html/body/div[6]/div[2]/ul/li[1]/div[2]/h2/a/text()')
    print(title_name)
    for i in range(len(title_name)):
        trans = str.maketrans("", "", " /n")  # 创建一个转换表，它将空格，逗号和感叹号映射为空字符
        s = title_name[i].translate(trans)  # 使用转换表删除这些字符
        print(s)

